package com.edu.controller.food;

import com.edu.entity.Food;
import com.edu.service.food.FoodService;
import com.edu.service.food.FoodServiceImpl;
import com.edu.utils.ParamFormat;
import com.edu.utils.ResponseBody;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Author ZCN
 * @Date 2021/11/13 18:02
 * @Version 1.0
 **/
@WebServlet("/food/add")
public class AddServlet extends HttpServlet {
    private FoodService foodService = new FoodServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            // 格式化utf-8
            ParamFormat.format(req,resp);
            //拿到数据
            Map<String, String[]> map = req.getParameterMap();
            Food food = new Food();
            BeanUtils.populate(food,map);
            foodService.insert(food);
            ResponseBody.success(resp,"success",0L,null);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseBody.failure(resp,e.getMessage());
        }
    }
}
